package o;

import android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.net.Uri;
import android.os.Handler;
import android.os.SystemClock;
import android.text.TextUtils;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Player.a;
import com.google.android.exoplayer2.Player.d;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.huawei.appmarket.support.audio.AudioBean;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import o.aij.c;

public class eoz {
    private OnAudioFocusChangeListener ʼ = new OnAudioFocusChangeListener(this) {
        final /* synthetic */ eoz ॱ;

        {
            this.ॱ = r1;
        }

        public void onAudioFocusChange(int i) {
            if (Logger.ˎ()) {
                Logger.ˏ("AudioExoPlayer", "onAudioFocusChange:" + i);
            }
            if (i == -2 || i == -1) {
                this.ॱ.ˋ(7);
                this.ॱ.ॱ();
            }
        }
    };
    private final Runnable ʽ = new Runnable(this) {
        final /* synthetic */ eoz ˎ;

        {
            this.ˎ = r1;
        }

        public void run() {
            if (this.ˎ.ˏ != null) {
                this.ˎ.ʼ();
                long uptimeMillis = SystemClock.uptimeMillis();
                uptimeMillis += 1000 - (uptimeMillis % 1000);
                if (this.ˎ.ˏ.ʽ() == 0) {
                    this.ˎ.ॱ.removeCallbacksAndMessages(null);
                } else {
                    this.ˎ.ॱ.postAtTime(this.ˎ.ʽ, uptimeMillis);
                }
            }
        }
    };
    private abu ˊ;
    private epd ˋ;
    d ˎ = new a(this) {
        final /* synthetic */ eoz ˋ;

        {
            this.ˋ = r1;
        }

        public void ˎ(boolean z) {
            if (Logger.ˎ()) {
                Logger.ˎ("AudioExoPlayer", "onLoadingChanged isLoading=" + z);
            }
            this.ˋ.ॱॱ = z;
            if (this.ˋ.ˏ != null && this.ˋ.ˊ != null) {
                int ˋ = this.ˋ.ˊ.ˋ();
                this.ˋ.ˏ.ॱॱ(ˋ);
                if (this.ˋ.ˋ != null) {
                    this.ˋ.ˋ.ˋ(ˋ);
                }
            }
        }

        public void ˏ(boolean z, int i) {
            if (Logger.ˎ()) {
                Logger.ˎ("AudioExoPlayer", "onPlayerStateChanged playWhenReady=" + z + ",playbackState=" + i);
            }
            if (this.ˋ.ˏ != null && this.ˋ.ˊ != null) {
                if (i == 2) {
                    int ˋ = this.ˋ.ˊ.ˋ();
                    this.ˋ.ˏ.ॱॱ(ˋ);
                    if (this.ˋ.ˋ != null) {
                        this.ˋ.ˋ.ˋ(ˋ);
                    }
                } else if (i == 3) {
                    if (z) {
                        this.ˋ.ˏ.ˋ(1);
                        this.ˋ.ॱ.removeCallbacks(this.ˋ.ʽ);
                        this.ˋ.ॱ.post(this.ˋ.ʽ);
                        epi.ˏ().ॱ(true);
                    } else {
                        this.ˋ.ˏ.ˋ(0);
                        epi.ˏ().ॱ(false);
                    }
                    if (this.ˋ.ˋ != null) {
                        this.ˋ.ˏ.ॱ((int) this.ˋ.ˊ.ᐝ());
                        this.ˋ.ˋ.ˊ(this.ˋ.ˏ.ᐝ());
                    }
                } else if (i == 4) {
                    if (Logger.ˎ()) {
                        Logger.ˎ("AudioExoPlayer", "onCompletion");
                    }
                    this.ˋ.ˏ.ˋ(0);
                    epi.ˏ().ॱ(false);
                }
            }
        }

        public void ˊ(ExoPlaybackException exoPlaybackException) {
            if (this.ˋ.ˏ != null && this.ˋ.ॱॱ()) {
                this.ˋ.ˋ(7);
                this.ˋ.ˋ();
                Logger.ˎ("AudioExoPlayer", "play audio error: type" + exoPlaybackException.ˎ + ",logId=" + this.ˋ.ˏ.ͺ() + ",serviceType=" + this.ˋ.ˏ.ॱॱ());
                esq.ॱ(exoPlaybackException.ˎ, exoPlaybackException.ˎ, this.ˋ.ˊ(), this.ˋ.ˏ.ͺ());
                dgh.ˋ("340402", evg.ˊ(exoPlaybackException.ˎ, this.ˋ.ˏ.ͺ(), this.ˋ.ˏ.ॱˊ(), this.ˋ.ˏ.ॱॱ(), this.ˋ.ˏ.ˊ()));
                if (this.ˋ.ˋ != null) {
                    this.ˋ.ˋ.ˋ(exoPlaybackException.ˎ, exoPlaybackException.getMessage());
                }
            }
        }

        public void ˊ() {
            if (Logger.ˎ()) {
                Logger.ˎ("AudioExoPlayer", "onSeekProcessed");
            }
        }
    };
    private AudioBean ˏ;
    Handler ॱ = new Handler();
    private boolean ॱॱ;
    private long ᐝ = -1;

    public eoz() {
        ʻ();
    }

    private void ʻ() {
        try {
            abi o_aax;
            if (azc.ˎ().ॱ() >= 17) {
                o_aax = new aax();
            } else {
                o_aax = new aax(new ali(true, WXMediaMessage.THUMB_LENGTH_LIMIT), 150000, 300000, 2500, 5000, -1, true);
            }
            alb defaultTrackSelector = new DefaultTrackSelector();
            Context ˋ = ContentManager.getInstant().getContext();
            this.ˊ = abd.ˋ(ˋ, new DefaultRenderersFactory(ˋ), defaultTrackSelector, o_aax);
            this.ˊ.ˏ(abv.ˋ);
            this.ˊ.ˏ(this.ˎ);
        } catch (Exception e) {
            Logger.e("AudioExoPlayer", "initMediaPlayer error: " + e.toString());
        }
    }

    private void ˏ(String str) {
        if (!TextUtils.isEmpty(ˊ()) && this.ˊ != null) {
            try {
                if (Logger.ˎ()) {
                    Logger.ˏ("AudioExoPlayer", "setDataSource");
                }
                this.ˊ.ˋ(ॱ(Uri.parse(str)));
                this.ˊ.ˋ(true);
                if (this.ˏ != null) {
                    this.ˊ.ˎ((long) this.ˏ.ʼ());
                }
            } catch (Exception e) {
                Logger.e("AudioExoPlayer", "openMediaPlayer exception:" + e.getMessage());
            }
        }
    }

    private aim ॱ(Uri uri) {
        return new c(new aln(ContentManager.getInstant().getContext(), "ua")).ॱ(uri);
    }

    public String ˊ() {
        return this.ˏ == null ? null : this.ˏ.ॱˊ();
    }

    public void ॱ(AudioBean audioBean) {
        if (audioBean != null) {
            if (this.ˏ == null || !this.ˏ.equals(audioBean)) {
                this.ˏ = audioBean;
                if (this.ˊ != null) {
                    this.ˊ.ˎ(true);
                }
            }
        }
    }

    public void ˎ() {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioExoPlayer", "play");
        }
        if (this.ˊ != null && this.ˏ != null) {
            ʽ();
            this.ˏ.ˋ(1);
            if (this.ˏ.ʼ() >= this.ˏ.ᐝ()) {
                this.ˏ.ˎ(0);
            }
            if (ॱॱ()) {
                if (Logger.ˎ()) {
                    Logger.ˏ("AudioExoPlayer", "isPlaying");
                }
                this.ˊ.ˎ((long) this.ˏ.ʼ());
            } else if (ˊॱ()) {
                if (Logger.ˎ()) {
                    Logger.ˏ("AudioExoPlayer", "isPaused");
                }
                this.ˊ.ˎ((long) this.ˏ.ʼ());
                this.ˊ.ˋ(true);
            } else {
                ˏ(ˊ());
            }
            epi.ˏ().ॱ(true);
            this.ᐝ = SystemClock.uptimeMillis();
        }
    }

    public void ˋ(epd o_epd) {
        this.ˋ = o_epd;
    }

    private void ʼ() {
        if (this.ˏ != null && this.ˊ != null) {
            synchronized (this) {
                long ʼ = this.ˊ.ʼ();
                if (this.ˊ.ˏ() == 4) {
                    ʼ = (long) this.ˏ.ᐝ();
                }
                this.ˏ.ˎ((int) ʼ);
                if (this.ॱॱ) {
                    int ˋ = this.ˊ.ˋ();
                    if (this.ˏ.ˊॱ() != ˋ) {
                        this.ˏ.ॱॱ(ˋ);
                        if (this.ˋ != null) {
                            this.ˋ.ˋ(ˋ);
                        }
                    }
                }
            }
            if (this.ˋ != null) {
                this.ˋ.ॱ(this.ˏ.ʼ(), this.ˏ.ᐝ());
            }
            if (this.ˏ.ʼ() >= this.ˏ.ᐝ() && this.ˏ.ᐝ() > 0) {
                this.ˏ.ˎ(this.ˏ.ᐝ());
                this.ˏ.ˋ(0);
                this.ˊ.ˋ(false);
                this.ˊ.ˎ(true);
                if (this.ˋ != null) {
                    this.ˋ.ˋ();
                }
            }
        }
    }

    private void ʽ() {
        AudioManager ˎ = epe.ˏ().ˎ();
        if (ˎ != null) {
            int requestAudioFocus = ˎ.requestAudioFocus(this.ʼ, 3, 2);
            if (Logger.ˎ()) {
                Logger.ˏ("AudioExoPlayer", "requestFocusResult=" + requestAudioFocus);
            }
        }
    }

    private void ᐝ() {
        AudioManager ˎ = epe.ˏ().ˎ();
        if (ˎ != null) {
            ˎ.abandonAudioFocus(this.ʼ);
            if (Logger.ˎ()) {
                Logger.ˏ("AudioExoPlayer", "abondonFocused");
            }
        }
    }

    public void ˋ() {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioExoPlayer", "pauseInternal");
        }
        if (this.ˏ != null) {
            this.ˏ.ˋ(0);
            if (ˏॱ()) {
                this.ˏ.ˎ(this.ˏ.ᐝ());
            } else {
                this.ˏ.ˎ((int) this.ˊ.ʼ());
            }
        }
        if (ॱॱ()) {
            this.ˊ.ˋ(false);
        }
        this.ॱ.removeCallbacks(this.ʽ);
        epi.ˏ().ॱ(false);
    }

    public void ॱ() {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioExoPlayer", "pause");
        }
        ˋ();
        if (this.ˋ != null) {
            this.ˋ.ˎ();
        }
        ᐝ();
        epe.ˏ().ॱˊ();
    }

    public void ˏ() {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioExoPlayer", "release");
        }
        ॱ();
        if (this.ˊ != null) {
            this.ˊ.ʽ();
            this.ˊ = null;
        }
        if (this.ˏ != null) {
            this.ˏ.ॱॱ(0);
        }
        this.ॱ.removeCallbacks(this.ʽ);
        epi.ˏ().ॱ(false);
    }

    public void ˏ(long j) {
        if (Logger.ˎ()) {
            Logger.ˏ("AudioExoPlayer", "seek position=" + j);
        }
        if (this.ˏ != null && this.ˊ != null) {
            if (j >= ((long) this.ˏ.ᐝ())) {
                this.ˏ.ˎ(this.ˏ.ᐝ());
                this.ˏ.ˋ(0);
            }
            this.ˊ.ˎ(j);
        }
    }

    private boolean ॱॱ() {
        if (this.ˊ != null) {
            boolean ˎ = this.ˊ.ˎ();
            int ˏ = this.ˊ.ˏ();
            if (ˏ == 3 || ˏ == 2) {
                return ˎ;
            }
        }
        return false;
    }

    private boolean ˊॱ() {
        if (this.ˊ == null) {
            return false;
        }
        int ˏ = this.ˊ.ˏ();
        boolean ˎ = this.ˊ.ˎ();
        if (ˏ == 1) {
            return false;
        }
        if (!ˎ || ˏ == 4) {
            return true;
        }
        return false;
    }

    private boolean ˏॱ() {
        boolean z;
        if (this.ˏ == null || this.ˏ.ʼ() < this.ˏ.ᐝ()) {
            z = false;
        } else {
            z = true;
        }
        boolean z2;
        if (this.ˊ == null || this.ˊ.ˏ() != 4) {
            z2 = false;
        } else {
            z2 = true;
        }
        if (z || r3) {
            return true;
        }
        return false;
    }

    public void ˋ(int i) {
        long uptimeMillis;
        if (this.ᐝ != -1) {
            uptimeMillis = SystemClock.uptimeMillis() - this.ᐝ;
            this.ᐝ = -1;
        } else {
            uptimeMillis = 0;
        }
        if (this.ˏ == null) {
            return;
        }
        if (uptimeMillis > 0 || i == 5) {
            if (Logger.ˎ()) {
                Logger.ˏ("AudioExoPlayer", "play time : logId=" + this.ˏ.ͺ() + ",playTime=" + uptimeMillis + ",serviceType=" + this.ˏ.ॱॱ() + ",playMode=" + i + ",logSource=" + this.ˏ.ˊ());
            }
            dgh.ˋ("340102", evg.ˋ(uptimeMillis, this.ˏ.ॱॱ(), this.ˏ.ͺ(), i, this.ˏ.ˊ()));
        }
    }
}
